<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

/**
 * 勋章逻辑
 */
class Money extends AdminBase
{
	/**
     * 添加
     */
    public function cjAdd($data = [])
    {
       
        $url = url('cjList');
		$data['create_time']=time();
        $result = $this->modelCj->setInfo($data);
        $result && action_log('新增', '新增幸运竞猜，username：' . $data['num']);        
        return $result ? [RESULT_SUCCESS, '幸运竞猜添加成功', $url] : [RESULT_ERROR, $this->modelUser->getError()];
    }
	/**
     * 竞猜编辑
     */
    public function cjEdit($data = [])
    {
        $url = url('cjList');
        
		$info=db('cj')->where('id',$data['id'])->value('status');
		if(!empty($info)){
			return [RESULT_ERROR, '已兑奖，不能编辑'];
		}
		$result = db('cj')->where('id',$data['id'])->update($data);
		$result && action_log('编辑', '编辑用户，id：' . $data['id']);  
        return $result ? [RESULT_SUCCESS, '用户编辑成功', $url] : [RESULT_ERROR, $this->modelUser->getError()];
    }
    /**
     * 获取勋章列表
     */
    public function getCjList($where = [], $field = '*', $order = '', $paginate = DB_LIST_ROWS)
    {
        $list= $this->modelCj->getList($where, $field, $order, $paginate);  
		foreach($list as $k=>$v){
			if(!empty($v['user_id'])){
				$list[$k]['username']=db('user')->where('id',$v['user_id'])->value('username');
				$list[$k]['name']=db('user')->where('id',$v['user_id'])->value('name');
			}else{
				$list[$k]['username']='';
				$list[$k]['name']='';
			}			
		}
        return $list;
    }
    /**
     * 获取勋章信息
     */
    public function getMoneyInfo($data)
    {
        
        $info = $this->modelMoney->getInfo($data);
        
        $info['leader_nickname'] = $this->modelMoney->getValue(['id' => $info['leader_id']], 'nickname');
        
        return $info;
    }
	/**
     * 获取勋章转让列表
     */
    public function getMoneyZrList($where = [], $field = 'm.*,b.username,b.name', $order = 'm.create_time desc', $paginate = DB_LIST_ROWS)
    {
        
        $this->modelMoneyZr->alias('m');
        
        $join = [
                    [SYS_DB_PREFIX . 'user b', 'm.user_id = b.id', 'LEFT'],
                ];
        
   
        
        $this->modelMoneyZr->join = $join;
        
        return $this->modelMoneyZr->getList($where, $field, $order, $paginate);
    }
    /**
     * 获取勋章列表
     */
    public function getMoneyOrderList($where = [], $field = 'm.*,b.username,b.name', $order = 'm.create_time desc', $paginate = DB_LIST_ROWS)
    {
        
        $this->modelMoneyOrder->alias('m');
        
        $join = [
                    [SYS_DB_PREFIX . 'user b', 'm.user_id = b.id', 'LEFT'],
                ];
        
        $where['m.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        $this->modelMoneyOrder->join = $join;
        
        return $this->modelMoneyOrder->getList($where, $field, $order, $paginate);
    }
    /**
     * 获取勋章列表
     */
    public function getMoneyList($where = [], $field = 'm.*,b.username,b.name', $order = '', $paginate = DB_LIST_ROWS)
    {
        
        $this->modelMoney->alias('m');
        
        $join = [
                    [SYS_DB_PREFIX . 'user b', 'm.user_id = b.id', 'LEFT'],
                ];
        
        $where['m.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        $this->modelMoney->join = $join;
        
        return $this->modelMoney->getList($where, $field, $order, $paginate);
    }
    
    /**
     * 导出勋章列表
     */
    public function exportMoneyList($where = [], $field = 'm.*,b.username,b.name', $order = '')
    {
        
        $list = $this->getMoneyList($where, $field, $order, false);
        
        $titles = "编号,用户名,姓名,拥有时间";
        $keys   = "code,username,name,create_time";
        
        action_log('导出', '导出勋章列表');
        
        export_excel($titles, $keys, $list, '勋章列表');
    }
    /**
     * 获取勋章列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        !empty($data['user_id']) && $where['m.user_id'] = $data['user_id']; 
        !empty($data['search_data']) && $where['m.code|b.name|b.username'] = ['like', '%'.$data['search_data'].'%'];       
        return $where;
    }
	/**
     * 状态改变
     */
    public function moneyStatus($param = [])
    {
        $param['page']=!empty($param['page'])?$param['page']:1;
        $url = '/admin.php/money/moneylist?page='.$param['page'];
        if($param['status'] == 1){
			$result=db('money')->where('id',$param['id'])->update(array('status'=>0));
		}else{
			$result=db('money')->where('id',$param['id'])->update(array('status'=>1));
		}              
        $result && action_log('状态', '勋章状态，where：' . http_build_query($param));
        
        return $result ? [RESULT_SUCCESS, '勋章状态修改成功', $url] : [RESULT_ERROR, $this->modelMoney->getError(), $url];
    }
	 /**
     * 获取勋章列表
     */
    public function getMoneyLogList($where = [], $field = 'm.*,b.username,b.name', $order = '', $paginate = DB_LIST_ROWS)
    {
        
        $this->modelMoneyLog->alias('m');
        
        $join = [
                    [SYS_DB_PREFIX . 'user b', 'm.user_id = b.id', 'LEFT'],
					[SYS_DB_PREFIX . 'user c', 'm.getid = c.id', 'LEFT'],
                ];
        
        $where['m.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        $this->modelMoneyLog->join = $join;
        
        return $this->modelMoneyLog->getList($where, $field, $order, $paginate);
    }
     /**
     * 获取勋章列表搜索条件
     */
    public function getWheres($data = [])
    {
        
        $where = [];        
        !empty($data['id']) && $where['m.money_id'] = $data['id'];       
        return $where;
    }
   
}
